package com.cskaoyan.servlet;

import com.cskaoyan.bean.CatAndBrandData;
import com.cskaoyan.bean.GoodsParameter;
import com.cskaoyan.bean.PageData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.GoodsCreateVo;
import com.cskaoyan.common.CommonServlet;
import com.cskaoyan.model.MarketGoods;
import com.cskaoyan.service.*;
import com.cskaoyan.service.impl.GoodsAttributeServiceImpl;
import com.cskaoyan.service.impl.GoodsProductServiceImpl;
import com.cskaoyan.service.impl.GoodsServiceImpl;
import com.cskaoyan.service.impl.GoodsSpecificationServiceImpl;
import com.cskaoyan.utils.JacksonUtil;
import com.cskaoyan.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


@WebServlet("/admin/goods/*")
public class AdminGoodsServlet extends CommonServlet {
    GoodsService goodsService = new GoodsServiceImpl();
    GoodsAttributeService goodsAttributeService = new GoodsAttributeServiceImpl();
    GoodsProductService goodsProductService = new GoodsProductServiceImpl();
    GoodsSpecificationService goodsSpecificationService = new GoodsSpecificationServiceImpl();

    //创建商品
    public BaseRespVo create(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        GoodsCreateVo goodsCreateVo = JacksonUtil.read(jsonStr, GoodsCreateVo.class);
        MarketGoods marketGoods = goodsCreateVo.getGoods();
        String goodsSn = marketGoods.getGoodsSn();
        String name = marketGoods.getName();
        if (goodsSn==null||goodsSn.isEmpty()||name==null||name.isEmpty()) {
            return BaseRespVo.fail("参数不对", 401);
        }
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        goodsService.create(sqlSession,marketGoods);
        goodsAttributeService.create(sqlSession,goodsCreateVo.getAttributes(),marketGoods.getId());
        goodsProductService.create(sqlSession,goodsCreateVo.getProducts(),marketGoods.getId());
        goodsSpecificationService.create(sqlSession,goodsCreateVo.getSpecifications(),marketGoods.getId());
        sqlSession.commit();
        sqlSession.close();
        return BaseRespVo.ok(null);
    }

    // 查看商品列表
    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        // 获取解析key-value类型的请求参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        GoodsParameter goodsParameter= GoodsParameter.init(parameterMap);

        // 业务处理
        PageData data = goodsService.goodsList(goodsParameter);

        // 结果响应,封装BaseRespVo
        return BaseRespVo.ok(data);
    }


    // 更新商品信息
    public BaseRespVo update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 此时的请求参数为json,则需要下列方法来进行参数的接收
        // 参数接收
        String jsonStr = request.getReader().readLine();
        // JsonStr对象成对应的对象,封装请求参数
        GoodsCreateVo goodsCreateVo = JacksonUtil.read(jsonStr, GoodsCreateVo.class);

        //做业务处理
        BaseRespVo baseRespVo = goodsService.updateGoods(goodsCreateVo);
        //做出响应
        return baseRespVo;

    }

    //查看商品信息
    public BaseRespVo detail(HttpServletRequest request, HttpServletResponse response) {
        // 获取解析key-value类型的请求参数
        Integer id =Integer.parseInt(request.getParameter("id"));


        // 业务处理
        BaseRespVo baseRespVo = goodsService.detailGoods(id);

        // 结果响应,封装BaseRespVo
        return baseRespVo;
    }

    //显示所有的所属分类(market_category)和所属品牌商(market_brand)
    public BaseRespVo catAndBrand(HttpServletRequest request, HttpServletResponse response) {
        // 业务处理
        CatAndBrandData data = goodsService.catAndBrand();

        // 结果响应,封装BaseRespVo
        return BaseRespVo.ok(data);
    }

    //删除商品
    public BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 此时的请求参数为json,则需要下列方法来进行参数的接收
        // 参数接收
        String jsonStr = request.getReader().readLine();
        // JsonStr对象成对应的对象,封装请求参数;此时封装为MarketGoods对象来接收
        MarketGoods marketGoodes = JacksonUtil.read(jsonStr, MarketGoods.class);

        //业务删除
        BaseRespVo baseRespVo = goodsService.deleteGoods(marketGoodes);

        //响应返回
        return baseRespVo;

    }




}
